package cn.edu.java.thread.原子类;

import java.util.concurrent.atomic.AtomicLongArray;

public class AtomicLongArrayDemo {
    public static void main(String[] args) {
        // 创建一个长度为5的AtomicLongArray对象
        AtomicLongArray atomicLongArray = new AtomicLongArray(5);

        // 设置数组元素的值
        atomicLongArray.set(0, 0);
        atomicLongArray.set(1, 10);
        atomicLongArray.set(2, 20);
        atomicLongArray.set(3, 30);
        atomicLongArray.set(4, 40);

        // 获取数组元素的值
        long value1 = atomicLongArray.get(1);
        System.out.println("Element at index 1: " + value1);

        // 原子增加数组元素的值
        long newValue = atomicLongArray.incrementAndGet(2);
        System.out.println("New value at index 2: " + newValue);

        // 原子减少数组元素的值
        long updatedValue = atomicLongArray.decrementAndGet(3);
        System.out.println("Updated value at index 3: " + updatedValue);

        // 原子更新数组元素的值
        long previousValue = atomicLongArray.getAndSet(0, 100);
        System.out.println("Previous value at index 0: " + previousValue);
        System.out.println("Current value at index 0: " + atomicLongArray.get(0));
    }
}

